//
//  EDSCacheManager.m
//  DriveSchoolCoach
//
//  Created by TangBin on 2019/8/13.
//  Copyright © 2019 TangBin. All rights reserved.
//

#import "EDSCacheManager.h"

static NSString *const EDSCacheManagerFile = @"EDSStudentStudentManagerFile";

@interface EDSCacheManager ()
@property (nonatomic, strong) YYCache *yyCache;
@end

@implementation EDSCacheManager

+ (instancetype)shareCache
{
    static EDSCacheManager *cache = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        cache = [[EDSCacheManager alloc]init];
        NSString *cacheFolder = [NSFileManager fileLibraryPath];
        NSString *path = [cacheFolder stringByAppendingPathComponent:EDSCacheManagerFile];
        cache.yyCache = [[YYCache alloc]initWithPath:path];
    });
    return cache;
}

/**
 根据key存储object
 
 @param object 存储内容
 @param key 存储key
 */
- (void)storeObject:(id<NSCoding>)object forKey:(NSString *)key
{
//    YYCache *cacheManager = [[self shareCache] yyCache];
    [_yyCache setObject:object forKey:key];
}

/**
 根据key来读取内容
 
 @param key 要读取内容的key
 @return 内容
 */
- (id)objectForKey:(NSString *)key
{
    
//    YYCache *cacheManager = [[self shareCache] yyCache];
    return [_yyCache objectForKey:key];
}

- (void)clearCache
{
    [_yyCache removeAllObjects];
}

/**
 根据key来移除内容
 
 @param key 需移除内容的key
 */
- (void)removeStoreObjectForKey:(NSString *)key
{
//    YYCache *cacheManager = [[self shareCache] yyCache];
    [_yyCache removeObjectForKey:key];
}


/**
 清除缓存
 */
- (void)cleanCacheSize;
{
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
        }
    }
    //清除后读取缓存大小
    //    return [self readCacheSize];
}


#pragma mark - 读取缓存大小
//获取缓存文件的大小
- (NSString *)readCacheSize
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
    CGFloat cacheSize = [ self folderSizeAtPath :cachePath];
    return [NSString stringWithFormat:@"%.2fM",cacheSize];
}

// 遍历文件夹获得文件夹大小，返回多少 M
- ( float )folderSizeAtPath:( NSString *)folderPath
{
    
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //获取文件全路径
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    return folderSize/( 1024.0 * 1024.0);
}

// 计算 单个文件的大小
- ( long long )fileSizeAtPath:( NSString *)filePath
{
    NSFileManager * manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath])
    {
        return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];
    }
    return 0;
}

@end
